package com.jt.basic.thread;

import jdk.internal.dynalink.beans.StaticClass;

import java.util.Arrays;
import java.util.List;
import java.util.concurrent.CopyOnWriteArrayList;

public class LocalCache01 {
    //底层是乐观锁
    private static CopyOnWriteArrayList<String> cache =
            new CopyOnWriteArrayList<>();
    static List<String> getData(){


            if (cache.isEmpty()) {
                System.out.println("load data from database");
                List<String> data = Arrays.asList("A", "B", "C");
                cache.addAll(data);
            }


        return cache;
    }
    public static void main(String[] args) {
        Thread t1 = new Thread(new Runnable() {
            @Override
            public void run() {
                System.out.println(getData());
            }
        });
        Thread t2 = new Thread(new Runnable() {
            @Override
            public void run() {
                System.out.println(getData());
            }
        });
        Thread t3 = new Thread(new Runnable() {
            @Override
            public void run() {
                System.out.println(getData());
            }
        });
        t1.start();
        t2.start();
        t3.start();
    }
}
